<template>
  <view>
    <web-view :src="webUrl"></web-view>
  </view>
</template>

<script>
var app = getApp()

export default {
  data() {
    return {
      webUrl: '',
      token: '',
      loading: false
    }
  },

  onLoad: function (options) {
    this.token = app.globalData.gtktoken

    if (options.url && options.token) {
      this.setlogin()
      // 传参方式1,url和token分开传
      let decodedUrl = decodeURIComponent(options.url)
      // 判断url上是否有其他参,决定token拼接方式
      let num = decodedUrl.indexOf('?')
      num === -1 ? (this.webUrl = `${decodedUrl}?token=${this.token}`) : (this.webUrl = `${decodedUrl}&token=${this.token}`)
    } else if (options.url && options.forget) {
      let decodedUrl = decodeURIComponent(options.url)
      // 判断url上是否有其他参,决定token拼接方式
      let num = decodedUrl.indexOf('?')
      num === -1 ? (this.webUrl = `${decodedUrl}?forget=${options.forget}`) : (this.webUrl = `${decodedUrl}&forget=${options.forget}`)
    } else if (options.url) {
      if (options.decodeURI) {
        this.webUrl = options.url
      } else {
        this.webUrl = decodeURIComponent(options.url)
      }
    }

    if (options.title) {
      uni.setNavigationBarTitle({
        title: options.title
      })
    }
  },
  methods: {
    setlogin() {
      var that = this
      app.post(
        'ApiIndex/logintogether',
        {
          gtksession: that.token
        },
        function (res) {
          if (res.status == 0) {
            //登录过期\或者没有登录
            uni.$u.route({
              type: 'reLaunch',
              url: '/pages/index/login'
            })
          } else {
            that.getdata()
          }
        }
      )
    },
    getdata() {
      var that = this
      that.loading = true
      app.get('ApiMy/usercenter', {}, function (data) {})
    }
  }
}
</script>
